package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.blog.bean.Result;
import com.yun.blog.bean.Star;
import com.yun.blog.bean.dto.StarBlogDto;
import com.yun.blog.service.StarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * title        : StarController
 * projectName  : dantouBlog-back
 * description  : 收藏功能控制类
 * author       : simple
 * email        : yun_simple@163.com
 * date         : 2023/1/22上午12:16
 */

@Slf4j
@RestController
@RequestMapping("/star")
@Api(tags = "收藏文章接口")
public class StarController {

    @Resource
    StarService starService;

    /**
     * 根据博客id和用户id添加到收藏
     *
     * @param star 前端传递 用户id 和 博客id
     * @return Result
     **/
    @PostMapping("/addStar")
    @ApiOperation(value = "添加到收藏")
    public Result addStar(@RequestBody Star star) {
        if (starService.isStar(star.getUserId(), star.getBlogId())) {
            return Result.fail("收藏失败！该博客已经被收藏。");
        }

        try {
            starService.save(star);
            return Result.success("收藏成功！");
        } catch (Exception e) {
            // throw new RuntimeException(e);
            return Result.fail("收藏失败！错误信息为：" + e.getMessage());
        }
    }

    /**
     * 检查是否已经收藏
     *
     * @param userId 用户id
     * @param blogId 博客id
     * @return java.lang.Boolean 是否已经收藏
     **/
    @GetMapping("/isStar")
    @ApiOperation("检查是否已经被收藏")
    public Boolean isStar(Integer userId, Integer blogId) {
        return starService.isStar(userId, blogId);
    }

    /**
     * 删除收藏
     *
     * @param star 前端传递 用户id 和 博客id
     * @return com.yun.blog.bean.Result
     **/
    @PostMapping("/delStar")
    @ApiOperation("删除收藏")
    public Result delStar(@RequestBody Star star) {
        if (!starService.isStar(star.getUserId(), star.getBlogId())) {
            return Result.fail("收藏失败！该博客没有被收藏。");
        }

        try {
            LambdaQueryWrapper<Star> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(Star::getUserId, star.getUserId()).eq(Star::getBlogId, star.getBlogId());
            starService.remove(queryWrapper);
            return Result.success("取消收藏成功！");
        } catch (Exception e) {
            // throw new RuntimeException(e);
            return Result.fail("取消收藏失败！错误信息为：" + e.getMessage());
        }
    }

    /**
     * 获取收藏列表
     *
     * @param pageSize 分页大小
     * @param pageNum 当前页
     * @param userId 收藏家的id
     * @return  com.yun.blog.bean.Result
     **/
    @GetMapping("/getStarBlog")
    @ApiOperation("获取收藏列表")
    public Result getStarBlog(@RequestParam(defaultValue = "15") Integer pageSize,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam() Integer userId) {
        try {
            Result success = Result.success();
            Page<StarBlogDto> page = starService.getStarBlog(new Page<>(pageNum, pageSize),userId);
            success.add("page", page);
            return success;
        } catch (Exception e) {
            log.error("获取失败：错误信息为：{}", e.getMessage());
            return Result.fail("获取失败：错误信息为：" + e.getMessage());
        }

    }

}
